<template>
  <div class="home">
    <!-- 查询开始 -->
    <div class="top">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="新闻">
          <el-input class="input" v-model="formInline.user" placeholder="新闻名"></el-input>
        </el-form-item>
        <el-form-item label="公告类型" class="gonggao">
          <el-select class="input" v-model="formInline.region" placeholder="请选择">
            <el-option label="服务协议" value="shanghai"></el-option>
            <el-option label="咨询" value="beijing"></el-option>
            <el-option label="小区公告" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="chaxun" type="primary" @click="cha">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-button class="button" @click="announceAdd = true" type="primary">添加</el-button>
      <el-button class="button" type="danger" @click="moredelete()">批量删除</el-button> -->
    </div>
    <!-- 查询结束 -->
    <!-- 添加弹出框开始 -->
    <!-- <el-dialog title="添加公告" :visible.sync="announceAdd" width="70%" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" prop="copyContent">
          <UEditor :content="form.copyContent" @ueChange="ueChange" />
        </el-form-item>
        <el-form-item label="公告图片" prop="picture">
          <el-upload
            class="avatar-uploader"
            action="http://estate.eshareedu.cn/estate/upload/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio label="1">开始</el-radio>
            <el-radio label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option label="服务协议" value="1"></el-option>
            <el-option label="咨询" value="2"></el-option>
            <el-option label="小区公告" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="announceAdd = false">取 消</el-button>
        <el-button type="primary" @click="anadd">添 加</el-button>
      </span>
    </el-dialog> -->
    <!-- 添加弹出框结束 -->
    <center ></center>
    <!-- table表格开始 -->
    <!-- <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :header-cell-style="tableRowHdBg"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" align="center" label="id"></el-table-column>
        <el-table-column prop="typename" align="center" label="类型">
          <template slot-scope="scope">
            <span
              @click="leixing(scope.row)"
              style="color:#2981ff;text-decoration:underline;"
            >{{scope.row.typename}}</span>
            <el-dialog title="详情" :visible.sync="centerDialogVisible" width="50%" center>
              <div class="home">
                这当然是详情了
              </div>
            </el-dialog>
          </template>
        </el-table-column>
        <el-table-column prop="title" align="center" label="标题"></el-table-column>
        <el-table-column prop="picture" align="center" label="图片"></el-table-column>
        <el-table-column prop="state" align="center" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="addtime" align="center" label="发布时间" width="150px"></el-table-column>
        <el-table-column prop="edituser" align="center" label="发布人"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <el-row slot-scope="scope">
            <el-button type="success">修改</el-button>
            <el-button type="danger" @click="handleMulDel(scope.row.id)">删除</el-button>
          </el-row>
        </el-table-column>
      </el-table>
    </div> -->
    <tab></tab>
    <!-- table表格结束 -->
    <!-- 分页开始 -->
    <div class="block" v-if="tableData.length > 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[2, 5, 10, 15]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        align="center"
      ></el-pagination>
    </div>
    <!-- 分页结束 -->
  </div>
</template>

<script>

import { newsadd, newsList ,newdelete,newdeleteall} from "../../api/user";
import UEditor from './../common/UEditor.vue'
import center from '../../views/Fee.vue'
import tab from '../../views/Table'

export default {
  components: { UEditor ,center},
  data() {
    return {
      // 添加
      form: {
        id: 0,
        title: "",
        picture: "2021/10/22/202110221150137894894.jpg",
        edituser: "",
        state: "",
        type: "",
        hits: "",
        // 富文本编辑器
        copyContent: '默认文本',
        content:"",
        addtime: "2021-10-25 09:56:47"
      },
      // 添加弹框
      announceAdd: false,
      // 弹框
      centerDialogVisible: false,
      // 查询
      formInline: {
        user: "",
        region: ""
      },
      // 上传图片
      imageUrl: "",
      // 分页
      page: 1,
      psize: 3,
      total: 0,
      // state: true,
      tableData: [],
      multipleSelection: [],
      newdeleteall:[]
    };
  },
  mounted() {
    this.getnewsList();
  },
  methods: {
    //批量
    moredelete(){
      let ids=this.multipleSelection
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          newdeleteall({ids}).then((res)=>{
            console.log(11)
            if(res.errCode==10000){
              this.getnewsList()
            }
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //删除
    handleMulDel(id){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        newdelete({id:id}).then((res)=>{
        if(res.errCode==10000){
        this.getnewsList()
        }
        })
        this.$message({
        type: 'success',
        message: '删除成功!'
        });
    }).catch(() => {
        this.$message({
        type: 'info',
        message: '已取消删除'
        });          
    });
    },
    // 富文本编辑器
    ueChange (data) {
      this.form.copyContent = data
    },
    // 添加确认按钮
    async anadd() {
      const edituser = sessionStorage.getItem("name")
      const params = {
        id: 0,
        title: this.form.title,
        picture: this.form.picture,
        edituser: edituser,
        state: this.form.state,
        type: this.form.type,
        hits: this.form.hits,
        content:this.form.copyContent,
        addtime: this.form.addtime
      }
      let res = await newsadd(params);
      // console.log(1111, res);
      if (res.errCode === 10000) {
        this.announceAdd = false
        this.getnewsList();
        this.$refs['form'].resetFields()
      }
    },
    // 添加弹框的关闭按钮
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 渲染列表
    async getnewsList() {
      let res = await newsList({
        page: this.page,
        psize: this.psize
      });
      console.log(res);

      if (res.errCode === 10000) {
        this.tableData = res.data.list;
        this.total = res.data.counts;
      }
    },
        handleSelectionChange(val) {
      this.multipleSelection = [];
      for(let selectedItem of val){
        this.multipleSelection.push(selectedItem.id)
        console.log(this.multipleSelection);
      }
    },
    // 类型
    leixing() {
      this.centerDialogVisible = true;
    },
    // 分页
    handleSizeChange(val) {
      this.psize = val;
      this.getnewsList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getnewsList();
    },
    // 查询
    cha() {
      console.log("submit!");
    },
    // 行头的颜色
    tableRowHdBg({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background-color:rgb(244, 244, 244)";
      }
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
 
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style>
.top {
  margin-top: 30px;
  margin-left: 30px;
}
.table {
  padding: 20px;
  margin-left: 5px;
}
.input {
  width: 120px;
  height: 40px;
}
.gonggao {
  margin-left: 10px;
}
.chaxun {
  margin-left: 30px;
  width: 90px;
}
.button {
  width: 100px;
  height: 40px;
}
.block-pager {
  margin-top: 30px;
  padding-bottom: 20px;
}
.el-dialog__header {
  background-color: #fff;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>